############################################################################
#
# footer.mak
#
# 2018-08-20		zhangtao		File created
#
############################################################################
# Define source directory by default if not set
ifndef SRCDIR
SRCDIR = $(shell pwd)
endif

# Convert path to absolute path
TOPDIR := $(abspath $(TOPDIR))
SRCDIR := $(abspath $(SRCDIR))

# Define build directory for temporary objects
ifeq ($(debug), 1)
BUILDTYPE = debug
else
BUILDTYPE = release
endif


BUILDDIR = $(TOPDIR)/.build/$(PRODUCT_NAME)/object/$(BUILDTYPE)
TARGETDIR = $(TOPDIR)/.build/$(PRODUCT_NAME)/target/$(BUILDTYPE)/$(TARGETTYPE)


TARGET := $(TARGETDIR)/$(TARGET)
RELTARGET := $(subst $(TOPDIR)/,,$(TARGET))

# Find necessary files
DIR = $(shell find $(SRCDIR) -type d)

ifndef SRC
SRC = $(foreach subdir,$(DIR),$(wildcard $(subdir)/*.c))
SRC += $(foreach subdir,$(DIR),$(wildcard $(subdir)/*.cpp))
else
SRC := $(foreach item,$(SRC),$(abspath $(item)))
endif

ifeq ($(MERGE), 1)
MERGE_LIBS = $(shell find $(TOPDIR)/libmerge/libs -maxdepth 1 -type f -name *.a)
endif

ifndef INC
INC = $(foreach subdir,$(DIR),$(wildcard $(subdir)/*.h))
endif

ifndef LIBS
LIBS = $(foreach subdir,$(abspath $(TARGETDIR)/../lib),$(wildcard $(subdir)/*.a))
endif

# Set object files and depend files
OBJS_TMP = $(patsubst $(TOPDIR)/%.c,$(BUILDDIR)/%.c.o,$(SRC))
OBJS = $(patsubst $(TOPDIR)/%.cpp,$(BUILDDIR)/%.cpp.o,$(OBJS_TMP))
DEPS_TMP = $(patsubst $(TOPDIR)/%.c,$(BUILDDIR)/%.c.d,$(SRC))
DEPS = $(patsubst $(TOPDIR)/%.cpp,$(BUILDDIR)/%.cpp.d,$(DEPS_TMP))

# Specify phony targets
.PHONY: all enter target install uninstall clean

# All rules
all: target

ifeq ($(TARGETTYPE), bin)
ifdef BINCOPYDIR
TARGETCOPYDIR = $(BINCOPYDIR)
endif
$(TARGET): $(OBJS) $(LIBS) $(EXLIBS)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(RM) $@
	$(Q)$(CC) -o $@ -Wl,--start-group $(LDFLAGS) $^ -Wl,--end-group
ifneq ($(debug), 1)
	$(Q)$(STRIP) $@
endif
else ifeq ($(TARGETTYPE), lib)
ifeq ($(MERGE), 1)
ifdef MERGELIBCOPYDIR
TARGETCOPYDIR = $(MERGELIBCOPYDIR)
endif
ifdef PRJLIBDIR
PRJCOPYDIR = $(PRJLIBDIR)
endif
$(TARGET):$(OBJS)
	$(Q)mkdir -p $(dir $@)/tmp
	$(Q)cd $(dir $@)/tmp; for i in $(MERGE_LIBS); do $(CROSS)ar x $$i || exit; done; cd - > /dev/null
	$(Q)$(RM) $@
	$(Q)$(AR) $(ARFLAGS) $@ $^ $(dir $@)/tmp/*.o
	$(Q)rm -rf $(dir $@)/tmp
ifneq ($(debug), 1)
	$(Q)$(RANLIB) $@
endif
else
ifdef LIBCOPYDIR
TARGETCOPYDIR = $(LIBCOPYDIR)
endif
$(TARGET): $(OBJS)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(RM) $@
	$(Q)$(AR) $(ARFLAGS) $@ $^
ifneq ($(debug), 1)
	$(Q)$(RANLIB) $@
endif
endif
else ifeq ($(TARGETTYPE), so)
ifeq ($(MERGE), 1)
ifdef MERGELIBCOPYDIR
TARGETCOPYDIR = $(MERGELIBCOPYDIR)
endif
$(TARGET):$(OBJS)
	$(Q)mkdir -p $(dir $@)/tmp
	$(Q)cd $(dir $@)/tmp; for i in $(MERGE_LIBS); do $(CROSS)ar x $$i || exit; done; cd - > /dev/null
	$(Q)$(RM) $@
	$(Q)$(CC) $(SHARED) $@ $^ $(dir $@)/tmp/*.o
	$(Q)rm -rf $(dir $@)/tmp
ifneq ($(debug), 1)
	$(Q)$(RANLIB) $@
endif
endif
else
ifeq ($(MERGE), 1)
$(TARGET): $(OBJS) $(MERGE_LIBS)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(RM) $@
	$(Q)$(CC) $(SHARED) $@ $^
ifneq ($(debug), 1)
	$(Q)$(RANLIB) $@
endif
else
$(TARGET): $(OBJS)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(RM) $@
	$(Q)$(CC) $(SHARED) $@ $^
ifneq ($(debug), 1)
	$(Q)$(RANLIB) $@
endif
endif
endif
# Depend file rules
-include $(DEPS)

# Object rules for c
$(BUILDDIR)/%.c.o: $(TOPDIR)/%.c
	$(Q)mkdir -p $(dir $@)
	$(Q)$(CC) $(CFLAGS) -MMD -c $< -o $@

# Object rules for c++
$(BUILDDIR)/%.cpp.o: $(TOPDIR)/%.cpp
	$(Q)mkdir -p $(dir $@)
	$(Q)$(CPP) $(CXXFLAGS) -MMD -c $< -o $@
	
# Enter rules
enter:
	$(Q)echo "  MAKE    $(RELTARGET)"
ifeq ($(INCCOPY), 1)
	$(Q)echo "  INCCOPY   $(subst $(TOPDIR)/,,$(abspath $(INCCOPYDIR)))"
	$(Q)cp -puv $(INC) $(INCCOPYDIR)
endif

# Target rules
target: enter $(PRETARGET) $(TARGET) $(POSTTARGET)
	
# Install rules
install: target
ifdef TARGETCOPYDIR
ifneq ($(NOINSTALL), 1)
	$(Q)echo "  INSTALL   $(RELTARGET) -> $(abspath $(TARGETCOPYDIR))"
	$(Q)[ -d $(TARGETCOPYDIR) ] || mkdir -p $(TARGETCOPYDIR)
	$(Q)install $(TARGET) $(TARGETCOPYDIR)
	#$(Q)cp  $(TARGETCOPYDIR)/$(TARGET) $(PRJCOPYDIR) -f	
endif
endif
ifdef PRJCOPYDIR
	$(Q)install $(TARGET) $(PRJCOPYDIR)
endif

	
# Uninstall rules
uninstall:
ifdef TARGETCOPYDIR
ifneq ($(NOINSTALL), 1)
	$(Q)echo "  RM        $(abspath $(TARGETCOPYDIR)/$(shell basename $(TARGET)))"
	$(Q)rm -f $(TARGETCOPYDIR)/$(shell basename $(TARGET))
endif
endif

# Clean rules
clean:
	$(Q)echo "  CLEAN     $(RELTARGET)"
	$(Q)rm -f $(OBJS) $(DEPS)
	$(Q)rm -f $(TARGET)
